package dao

import (
	"gin-ranking/api/auth/entity"
	"gin-ranking/api/auth/params"
	result2 "gin-ranking/api/auth/result"
	"gin-ranking/common/page"
	"gin-ranking/common/result"
	"gin-ranking/common/util"
	"gin-ranking/pkg/db"
	"time"
)

type UserDao struct{}

// Save 用户保存
func (UserDao) Save(userName string, password string) entity.User {

	user := entity.User{
		UserId:     util.SimpleUUID(),
		UserName:   userName,
		Password:   util.Md5ToString(password),
		CreateTime: util.HTime{Time: time.Now()},
		UpdateTime: util.HTime{Time: time.Now()},
	}
	err := db.DB.Create(&user).Error
	if err != nil {
		panic(result.FailMag(err.Error()))
	}
	return user
}

// Update 用户更新
func (UserDao) Update(param params.UserEditParam) {
	var user = entity.User{
		UserId:     param.UserId,
		UserName:   param.UserName,
		UpdateTime: util.HTime{Time: time.Now()},
	}
	err := db.DB.Updates(&user).Error
	if err != nil {
		panic(result.FailMag(err.Error()))
	}
}

// QueryUserByUsername 根据用户名查询用户
func (UserDao) QueryUserByUsername(userName string) result2.UserResult {
	var userResult result2.UserResult // 定义返回对象
	err := db.DB.Model(&entity.User{}).Select("*").Where("user_name = ?", userName).Find(&userResult).Error
	if err != nil {
		panic(result.FailMag(err.Error()))
	}
	return userResult
}

// QueryById 根据主键查询
func (UserDao) QueryById(id string) (userResult result2.UserResult) {
	db := db.DB.Table(" user a ").Select("a.*, b.token").Joins("LEFT JOIN `user_session` b ON a.user_id = b.user_id").Where("a.user_id = ?", id)
	err := db.Find(&userResult).Error
	if err != nil {
		panic(result.FailMag(err.Error()))
	}
	return userResult
}

// QueryPageList 分页查询
func (UserDao) QueryPageList(param params.UserQueryPageListParam) page.Result[[]result2.UserResult] {

	pageDto := page.Init(param.PageNo, param.PageSize) // 默认在第1页取10条

	db := db.DB.Table(" user a ").Select("a.*, b.token").Joins("LEFT JOIN `user_session` b ON a.user_id = b.user_id")

	if len(param.UserName) > 0 {
		db = db.Where("a.user_name like ?", "%"+param.UserName+"%")
	}

	var records []result2.UserResult // 定义返回对象
	var total int64                  // 总数
	err := db.Count(&total).Scopes(page.Paginate(&pageDto)).Find(&records).Error
	if err != nil {
		panic(result.FailMag(err.Error()))
	}
	return page.Data(pageDto, total, records)

	//var db = dao.DB
	//var records []result2.UserQueryPageListResult // 定义返回对象
	//var sql = "SELECT  q.token, w.user_id, w.user_name, w.password FROM user w LEFT JOIN user_session q ON w.user_id = q.user_id where 1=1"
	//return page.Paginate(db, sql, pageDto, records)
}

// QueryList 获取用户列表
func (UserDao) QueryList(param params.UserQueryListParam) []result2.UserResult {
	db := db.DB.Table(" user a ").Select("a.*, b.token").Joins("LEFT JOIN `user_session` b ON a.user_id = b.user_id")
	if len(param.UserName) > 0 {
		db = db.Where("a.user_name like ?", "%"+param.UserName+"%")
	}

	var records []result2.UserResult // 定义返回对象
	err := db.Find(&records).Error
	if err != nil {
		panic(result.FailMag(err.Error()))
	}
	return records
}

// DeleteByIds 根据主键删除
func (UserDao) DeleteByIds(ids []string) {
	db.DB.Delete(&entity.User{}, ids)
}
